var emailWindow;

function destroyEmail(){
	emailWindow.destroy();
}

function isIE () {
  var myNav = navigator.userAgent.toLowerCase();
  return (myNav.indexOf('msie') != -1) ? parseInt(myNav.split('msie')[1]) : false;
}


function openEmailPopup(idCandidato,idNews,tipo){

	var loadMask = new Ext.LoadMask(Ext.getBody(), {msg:"Attendere..."});
	loadMask.show();
	Ext.Ajax.request({
		disableCaching:true,
	    url: siteUrl+"&_eventId=loadEmailTemplate&idCandidato="+idCandidato+"&idNews="+idNews+"&tipo="+tipo+"&ajaxSource=true&fragments=content",
	    success: function(response){
	    	loadMask.hide();
	    	var editor = Ext.create('Ext.panel.Panel', {
	        	title: 'Composizione Email',
	        	height: 370,
	            width: 547,
	            margin: '0 0 0 0 ',
	        	layout: {
	                align: 'stretch',
	                type: 'vbox'
	            },
	        	defaults: { 
	        	   labelWidth: 100,
	        	   margin: '0 0 0 0 '
	        	},
	        	items: [
	                    {
	                    	flex: 1,                	
	                        xtype: 'htmleditor',
	                        itemId: 'myEditor',
	                        name: 'emailEditor',
	                        height: 200,
	                        style: 'background-color: white;',
	                        value: response.responseText.substring(
	                        		response.responseText.indexOf("<span id=\"emailDiv\" style=\"display:none\">")
	                        		+"<span id=\"emailDiv\" style=\"display:none\">".length,response.responseText.lastIndexOf("</span>"))
	                    }
	                ],
	               buttons: [{
	                   text: 'Invia',
	                   handler: function() {	                	   
	                	   var content = editor.items.items[0].value;
	    			       var temp="";
	    			       var i = 0;
	    			       for (i=0;i<content.length;i++){	    				   
	    			    	   temp = temp + "&#"+(content.charAt(i).charCodeAt(0))+";";	
	    			       }	    	                      	                	   
	    	               document.getElementById("emailHTMLId").value=temp;
	                       document.getElementById("eventId").value="sendEmail";
	                       emailWindow.hide();
	                       if(isIE())
	                    	   document.charset = 'UTF-8';
	                       Ext.override(Ext.MessageBox, {
	                           buttonText: { yes: "Si", no: "No" }
	                       });
	                       Ext.Msg.show({
	                    	   title:'Nascondi News?',
	                    	   msg: 'Vuoi pure nascondere la news selezionata?',
	                    	   buttons : Ext.MessageBox.YESNO,
	                    	   fn: function(btn){
	                               if(btn == 'yes'){	                            	   
	                                    document.getElementById("hideNewsId").value="true";
	                               }else{
	                            	   document.getElementById("hideNewsId").value="false";
	                               }
	                               loadMask.show();
	                               document.getElementById("newsRicerca").submit();
	                           }
	                    	});
	                                             
	                   }
	               },{
	                   text: 'Annulla',
	                   handler: function() {
	                       destroyEmail();
	                   }
	               }]
	        });
	    	emailWindow = Ext.create("Ext.Window",{
		    		title : '',
		    	    width : 550,                            
		    	    height: 400,
		    	    closable : true,                                                   
		    	    modal : true,				    
		    	    id : 'riepilogoWindow',
		    	    closeAction:'destroy',  
		    	    items:editor
		    	});
	    	document.getElementById("idCandidatoToNotify").value=idCandidato;
	    	document.getElementById("idNewsToHandle").value=idNews;
	    	emailWindow.show();
		}
	});
}
	
